package com.seventh.associationsystem.service;

import com.seventh.associationsystem.bean.Association;
import com.seventh.associationsystem.bean.AssociationStatus;
import com.seventh.associationsystem.bean.AssociationType;
import com.seventh.associationsystem.bean.User;
import com.seventh.associationsystem.repository.AssociationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssociationService {

    @Autowired
    private AssociationRepository associationRepository;

    public Page<Association> getAllPageable(Pageable pageable) {
        return associationRepository.findByStatus(AssociationStatus.OPEN, pageable);
    }

    public Page<Association> getByTypePageable(AssociationType type, Pageable pageable) {
        return associationRepository.findByTypeAndStatus(type, AssociationStatus.OPEN, pageable);
    }

    public Page<Association> getByUser(User user, Pageable pageable) {
        return associationRepository.findByMembersContainingAndStatus(user, AssociationStatus.OPEN, pageable);
    }

    public List<Association> getUncheckedAll() {
        return associationRepository.findByStatus(AssociationStatus.UNCHECKED);
    }

    public void add(Association association) {
        associationRepository.save(association);
    }

    public Association findByName(String name) {
        return associationRepository.findByName(name);
    }

    public Association getByUid(String uid) {
        return associationRepository.findByUid(uid);
    }

    public void update(Association association) {
        associationRepository.save(association);
    }
}
